package pr.iceworld.fernando.springbatchquartzlearning.configuration;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

@Configuration
public class MySpringQuartz {

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws Exception {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setQuartzProperties(quartzProperties());
        return schedulerFactoryBean;
    }

    private Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

// Same as above
//    private Properties quartzProperties() throws IOException {
//        Properties quartzProperties = new Properties();
//        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("quartz.properties");;
//        quartzProperties.load(is);
//        return quartzProperties;
//    }

    @Bean
    public MyApplicationContextUtil myApplicationContextUtil() {
        return new MyApplicationContextUtil();
    }
}
